package com.githan.bootsecurity.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author https://github.com/Han56
 * @date 2021/4/8 14:35
 */
@Controller
public class SecurityController {

    @GetMapping("/home")
    public String home(){
        return "home";
    }

    @GetMapping("/users")
    public String users(){
        return "users";
    }

    @GetMapping("/roles")
    public String roles(){
        return "roles";
    }

    @GetMapping("/others")
    public String others(){
        return "others";
    }

    @GetMapping("/login/error")
    public void loginError(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        AuthenticationException exception =
                (AuthenticationException) request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        try{
            response.getWriter().write(exception.toString());
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @GetMapping("/menus")
    public String menus(){
        return "menus";
    }


}
